<?php
/**
 * Joomla! 1.5 component wbdatabase
 *
 * @version $Id: view.html.php 2009-04-09 16:20:44 svn $
 * @author woomla
 * @package Joomla
 * @subpackage wbdatabase
 * @license GNU/GPL
 *
 * A personal database with some fields to remember logins and pin codes.
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the wbdatabase component
 */
class WbdatabaseViewOverzicht extends JView 
{
  function displayX($tpl = null) 
  {
    $model = $this->getModel();
    $items = $model->getData();
    $this->assignRef('items', $items);
    parent::display($tpl);
  }
  
  function display($tpl = null) 
  {
    global $mainframe, $option;
    
//    $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
//    $layout   = JRequest::getVar('layout');
//    $task   = JRequest::getVar('task');
//    $Itemid   =JRequest::getVar('Itemid', 0, '', 'int');
//    $dispatcher =& JDispatcher::getInstance();
//    $user     =& JFactory::getUser();
    $document   =& JFactory::getDocument();
//    $acl       =& JFactory::getACL();
//    $session   =& JFactory::getSession();

    $document->addStyleSheet('components/com_wbdatabase/assets/wbdatabase.css');
    

    $db         =& JFactory::getDBO();
    $filter_order   = $mainframe->getUserStateFromRequest( "$option.filter_order",    'filter_order',   'm.id', 'cmd' );
    $filter_order_Dir = $mainframe->getUserStateFromRequest( "$option.filter_order_Dir",  'filter_order_Dir', '',   'word' );
    $filter_state   = $mainframe->getUserStateFromRequest( "$option.filter_state",    'filter_state',   '',   'word' );
    $search       = $mainframe->getUserStateFromRequest( "$option.search",      'search',     '',   'string' );
    $search       = JString::strtolower( $search );

    $showcode = JRequest::getVar('showcode', 'off', 'default', 'string');
    $showcode = $showcode === 'off' ? false : true;


    $limit    = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
    $limitstart = $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );

    $where = array();

    if ( $filter_state )
    {
      if ( $filter_state == 'P' )
      {
        $where[] = 'm.published = 1';
      }
      else if ($filter_state == 'U' )
      {
        $where[] = 'm.published = 0';
      }
    }
    if ($search)
    {
      //$where[] = 'LOWER(m.title) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
      $searchstring = $db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
      $waar[] = 'LOWER(m.title) LIKE ' . $searchstring;
      $waar[] = 'LOWER(m.code) LIKE ' . $searchstring;
      $waar[] = 'LOWER(m.user) LIKE ' . $searchstring;
      $waar[] = 'LOWER(m.email) LIKE ' . $searchstring;
      $waar[] = 'LOWER(m.url) LIKE ' . $searchstring;
      $waar[] = 'LOWER(m.comment) LIKE ' . $searchstring;
      $waar[] = 'LOWER(m.tags) LIKE ' . $searchstring;
      $waar = ( count($waar) ? ' ( ' . implode(' OR ' , $waar) . ')' : '' );
      $where[] = $waar;     
    }

    $where    = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
    $orderby  = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;

    $query = 'SELECT COUNT(m.id)'
    . ' FROM #__wbdatabase AS m'
    . $where
    ;
    $db->setQuery( $query );
    $total = $db->loadResult();

    jimport('joomla.html.pagination');
    $pagination = new JPagination( $total, $limitstart, $limit );

    $query = 'SELECT m.*'
    . ' FROM #__wbdatabase AS m'
    . $where
    . ' GROUP BY m.id'
    . $orderby
    ;
    $db->setQuery( $query, $pagination->limitstart, $pagination->limit );
    $rows = $db->loadObjectList();

    if ($db->getErrorNum())
    {
      echo $db->stderr();
      return false;
    }

    // state filter
    $lists['state'] = JHTML::_('grid.state',  $filter_state );

    // table ordering
    $lists['order_Dir'] = $filter_order_Dir;
    $lists['order']   = $filter_order;

    // search filter
    $lists['search']= $search;

    $this->assignRef('user',    JFactory::getUser());
    $this->assignRef('lists',   $lists);
    $this->assignRef('items',   $rows);
    $this->assignRef('query',   $query);
    $this->assignRef('pagination',  $pagination);
    $this->assignRef('showcode', $showcode);




    //$items =& $this->get('Data');
    //$total = count($items);

    parent::display($tpl);
  }
  
}
?>